<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中央区域烟花效果</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            background: #000;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>

    <script>
        // 设置画布
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        // 当窗口调整大小时重设画布
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });

        // 集合
        const fireworks = [];
        const particles = [];
        const texts = [];

        // 需要显示的文字
        const chars = "甘迎".split('');
        let currentCharIndex = 0;

        // 定义中央安全区域 (窗口中间2/3区域)
        function getSafeAreaX() {
            const safeWidth = canvas.width * 2/3;
            const margin = (canvas.width - safeWidth) / 2;
            return {
                min: margin,
                max: canvas.width - margin,
                center: canvas.width / 2
            };
        }

        function getSafeAreaY() {
            const safeHeight = canvas.height * 2/3;
            const margin = (canvas.height - safeHeight) / 2;
            return {
                min: margin,
                max: canvas.height - margin,
                center: canvas.height / 2
            };
        }

        // 将值限制在中央安全区域内
        function constrainToSafeArea(x, y) {
            const safeX = getSafeAreaX();
            const safeY = getSafeAreaY();
            
            return {
                x: Math.min(Math.max(x, safeX.min), safeX.max),
                y: Math.min(Math.max(y, safeY.min), safeY.max)
            };
        }

        // 烟花类
        class Firework {
            constructor(x, y, targetX, targetY, char = null) {
                // 起始点
                this.x = x;
                this.y = y;
                this.startX = x;
                this.startY = y;

                // 确保目标在中央安全区域内
                const safeTarget = constrainToSafeArea(targetX, targetY);
                this.targetX = safeTarget.x;
                this.targetY = safeTarget.y;

                // 显示的字符
                this.char = char;

                // 轨迹
                this.trail = [];
                for (let i = 0; i < 5; i++) {
                    this.trail.push([this.x, this.y]);
                }

                // 颜色和大小
                this.hue = Math.floor(Math.random() * 360);
                this.brightness = 60 + Math.floor(Math.random() * 40);

                // 物理属性 - 速度加倍
                this.velocity = {
                    x: (this.targetX - this.x) * 0.024,
                    y: (this.targetY - this.y) * 0.024
                };
                this.gravity = 0.02;
                this.resistance = 0.95;
                this.exploded = false;
            }

            update(index) {
                // 更新轨迹
                this.trail.pop();
                this.trail.unshift([this.x, this.y]);

                // 移动烟花
                this.x += this.velocity.x;
                this.y += this.velocity.y;
                this.velocity.y += this.gravity;

                // 检查是否到达目标
                const distanceToTarget = 
                    Math.sqrt(Math.pow(this.targetX - this.x, 2) + Math.pow(this.targetY - this.y, 2));
                
                // 如果足够接近目标点或开始下落，爆炸
                if (!this.exploded && (distanceToTarget < 15 || this.velocity.y > 0)) {
                    this.explode();
                    this.exploded = true;
                    fireworks.splice(index, 1);
                }
            }

            draw() {
                ctx.strokeStyle = `hsl(${this.hue}, 100%, ${this.brightness}%)`;
                ctx.lineWidth = 3;
                ctx.beginPath();
                ctx.moveTo(this.trail[0][0], this.trail[0][1]);
                for (let i = 1; i < this.trail.length; i++) {
                    ctx.lineTo(this.trail[i][0], this.trail[i][1]);
                }
                ctx.stroke();
            }

            explode() {
                // 创建爆炸粒子
                const particleCount = 80 + Math.floor(Math.random() * 40);
                for (let i = 0; i < particleCount; i++) {
                    particles.push(new Particle(this.x, this.y, this.hue));
                }

                // 如果有字符，创建文字
                if (this.char) {
                    texts.push(new Text(this.x, this.y, this.char, this.hue));
                }
            }
        }

        // 爆炸粒子类
        class Particle {
            constructor(x, y, hue) {
                this.x = x;
                this.y = y;
                this.hue = hue + Math.random() * 30 - 15;
                this.brightness = 50 + Math.random() * 50;
                this.size = 1 + Math.random() * 3;
                this.alpha = 1;

                // 物理属性
                const angle = Math.random() * Math.PI * 2;
                const speed = 0.5 + Math.random() * 4;
                this.velocity = {
                    x: Math.cos(angle) * speed,
                    y: Math.sin(angle) * speed
                };
                this.gravity = 0.05;
                this.resistance = 0.96;
                this.decay = 0.01 + Math.random() * 0.02;

                // 轨迹
                this.trail = [];
                for (let i = 0; i < 5; i++) {
                    this.trail.push([this.x, this.y]);
                }
            }

            update(index) {
                // 更新轨迹
                this.trail.pop();
                this.trail.unshift([this.x, this.y]);

                // 应用阻力
                this.velocity.x *= this.resistance;
                this.velocity.y *= this.resistance;
                
                // 应用重力
                this.velocity.y += this.gravity;
                
                // 移动粒子
                this.x += this.velocity.x;
                this.y += this.velocity.y;
                
                // 淡出
                this.alpha -= this.decay;
                
                // 检查是否应该移除
                if (this.alpha <= 0) {
                    particles.splice(index, 1);
                    return false;
                }
                
                return true;
            }

            draw() {
                // 设置绘制样式
                ctx.fillStyle = `hsla(${this.hue}, 100%, ${this.brightness}%, ${this.alpha})`;
                
                // 绘制粒子
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fill();
                
                // 绘制轨迹
                if (this.alpha > 0.3) {
                    ctx.strokeStyle = `hsla(${this.hue}, 100%, ${this.brightness}%, ${this.alpha * 0.5})`;
                    ctx.lineWidth = this.size * 0.5;
                    ctx.beginPath();
                    ctx.moveTo(this.trail[0][0], this.trail[0][1]);
                    for (let i = 1; i < this.trail.length; i++) {
                        ctx.lineTo(this.trail[i][0], this.trail[i][1]);
                    }
                    ctx.stroke();
                }
            }
        }

        // 文字类
        class Text {
            constructor(x, y, text, hue) {
                this.x = x;
                this.y = y;
                this.text = text;
                this.hue = hue;
                this.size = Math.min(canvas.width, canvas.height) * 0.12; // 文字大小根据屏幕调整
                this.alpha = 0;
                this.life = 0;
                this.decay = 0.007;
            }

            update() {
                this.life++;
                
                // 淡入
                if (this.life < 30) {
                    this.alpha = Math.min(this.life / 30, 1);
                } 
                // 稳定显示
                else if (this.life < 120) {
                    // 保持大小和透明度
                } 
                // 淡出
                else {
                    this.alpha -= this.decay;
                }
                
                // 生命周期结束
                if (this.alpha <= 0) {
                    return false;
                }
                return true;
            }

            draw() {
                ctx.font = `bold ${this.size}px Arial, sans-serif`;
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                
                // 外发光
                ctx.shadowColor = `hsla(${this.hue}, 100%, 70%, ${this.alpha * 0.5})`;
                ctx.shadowBlur = 15;
                
                // 文字填充
                ctx.fillStyle = `hsla(${this.hue}, 100%, 70%, ${this.alpha})`;
                ctx.fillText(this.text, this.x, this.y);
                
                // 文字描边
                ctx.lineWidth = 2;
                ctx.strokeStyle = `hsla(${this.hue + 30}, 100%, 90%, ${this.alpha})`;
                ctx.strokeText(this.text, this.x, this.y);
                
                // 重置阴影
                ctx.shadowBlur = 0;
            }
        }

        // 在中央安全区域发射普通烟花
        function launchRandomFirework() {
            // 获取安全区域
            const safeX = getSafeAreaX();
            const safeY = getSafeAreaY();
            
            // 起始点（从安全区域底部发射，而不是整个屏幕底部）
            const startX = safeX.min + Math.random() * (safeX.max - safeX.min);
            const startY = safeY.max; // 使用安全区域的底部
            
            // 目标点（中央安全区域内随机位置）
            const targetX = safeX.min + Math.random() * (safeX.max - safeX.min);
            const targetY = safeY.min + Math.random() * (safeY.max - safeY.min);
            
            fireworks.push(new Firework(
                startX, 
                startY, 
                targetX, 
                targetY
            ));
        }

        // 发射带有文字的烟花 - 确保在中央安全区域
        function launchTextFirework() {
            // 获取当前字符
            const char = chars[currentCharIndex];
            currentCharIndex = (currentCharIndex + 1) % chars.length;
            
            // 获取安全区域
            const safeX = getSafeAreaX();
            const safeY = getSafeAreaY();
            
            // 起始点（从安全区域底部发射）
            const startX = safeX.min + Math.random() * (safeX.max - safeX.min);
            const startY = safeY.max;
            
            // 目标点（中央安全区域内随机位置）
            const targetX = safeX.min + Math.random() * (safeX.max - safeX.min);
            const targetY = safeY.min + Math.random() * (safeY.max - safeY.min);
            
            fireworks.push(new Firework(
                startX, 
                startY, 
                targetX, 
                targetY, 
                char
            ));
        }

        // 发射一系列文字烟花 - 在中央区域均匀分布
        function launchTextSequence() {
            if (chars.length === 0) return;
            
            // 计算安全区域
            const safeX = getSafeAreaX();
            const safeY = getSafeAreaY();
            
            // 计算字符间安全距离
            const availableWidth = safeX.max - safeX.min;
            const safeSpace = availableWidth * 0.7 / chars.length;
            const startPosX = safeX.center - ((chars.length - 1) * safeSpace / 2);
            
            for (let i = 0; i < chars.length; i++) {
                setTimeout(() => {
                    // 从安全区域底部发射
                    const launchX = safeX.min + Math.random() * (safeX.max - safeX.min);
                    const launchY = safeY.max;
                    
                    // 目标位置均匀分布在中央安全区域
                    const targetX = startPosX + i * safeSpace;
                    const targetY = safeY.center; // 中央区域垂直中心
                    
                    fireworks.push(new Firework(
                        launchX,
                        launchY,
                        targetX,
                        targetY,
                        chars[i]
                    ));
                }, i * 600);
            }
        }

        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            
            // 半透明覆盖，形成尾迹效果
            ctx.fillStyle = 'rgba(0, 0, 0, 0.09)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            // 更新和绘制烟花
            for (let i = fireworks.length - 1; i >= 0; i--) {
                fireworks[i].update(i);
                fireworks[i].draw();
            }
            
            // 更新和绘制粒子
            for (let i = particles.length - 1; i >= 0; i--) {
                if (particles[i].update(i)) {
                    particles[i].draw();
                }
            }
            
            // 更新和绘制文字
            for (let i = texts.length - 1; i >= 0; i--) {
                if (texts[i].update()) {
                    texts[i].draw();
                } else {
                    texts.splice(i, 1);
                }
            }
        }

        // 立即发射第一批烟花
        for (let i = 0; i < 3; i++) {
            setTimeout(() => {
                launchRandomFirework();
            }, i * 150);
        }

        // 立即发射文字烟花序列
        setTimeout(launchTextSequence, 400);

        // 设置定时器定期发射烟花
        setInterval(launchRandomFirework, 600);
        setInterval(launchTextFirework, 1000);
        setInterval(launchTextSequence, 3000);

        // 开始动画
        animate();
    </script>
</body>
</html>